home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GXSTROKE.C < prev    next >
C/C++ Source or Header  |  1992-03-23  |  25KB  |  728 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gxstroke.c */
  21. /* Path stroking procedures for Ghostscript library */
  22. #include "math_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxarith.h"
  27. #include "gxmatrix.h"
  28. #include "gzstate.h"
  29. #include "gzdevice.h"
  30. #include "gzcolor.h"            /* requires gxdevice.h */
  31. #include "gzline.h"
  32. #include "gzpath.h"
  33.  
  34. /* stroke_add uses the following global for its path: */
  35. private gx_path stroke_path_body;
  36. private gx_path *stroke_path;
  37.  
  38. /*
  39.  * Structure for a partial line (passed to the drawing routine).
  40.  * Two of these are required to do joins right.
  41.  * Each endpoint includes the two ends of the cap as well,
  42.  * and the deltas for square and round cap computation.
  43.  *
  44.  * The deltas (co, cdelta, ce) are in clockwise order in device space
  45.  * around the endpoint p: they are one-half the line width (suitably
  46.  * transformed) at 90 degrees counter-clockwise, straight ahead,
  47.  * and 90 degrees clockwise from the oriented line o->e,
  48.  * where "90 degrees" is measured in *user* coordinates.
  49.  * Note that the values at o are the negatives of the values at e.
  50.  *
  51.  * Initially, only o.p, e.p, o.cdelta, width, and thin are set.
  52.  * compute_caps fills in the rest when needed.
  53.  */
  54. typedef gs_fixed_point _ss *p_ptr;
  55. typedef struct endpoint_s {
  56.     gs_fixed_point p;        /* the end of the line */
  57.     gs_fixed_point co, ce;        /* ends of the cap, p +/- width */
  58.     gs_fixed_point cdelta;        /* +/- cap length */
  59. } endpoint;
  60. typedef endpoint _ss *ep_ptr;
  61. typedef struct partial_line_s {
  62.     endpoint o;            /* starting coordinate */
  63.     endpoint e;            /* ending coordinate */
  64.     gs_fixed_point width;        /* one-half line width, see above */
  65.     int thin;            /* true if minimum-width line */
  66. } partial_line;
  67. typedef partial_line _ss *pl_ptr;
  68.  
  69. /* Procedures that stroke a partial_line (the first argument). */
  70. /* If both partial_lines are non-null, the procedure creates */
  71. /* an appropriate join; otherwise, the procedure creates an */
  72. /* end cap.  If the first int is 0, the procedure also starts with */
  73. /* an appropriate cap. */
  74. private int stroke_add(P4(int, pl_ptr, pl_ptr, gs_state *));
  75. private int stroke_fill(P4(int, pl_ptr, pl_ptr, gs_state *));
  76.  
  77. /* Other forward declarations */
  78. private int stroke(P3(gx_path *,
  79.   int (*)(P4(int, pl_ptr, pl_ptr, gs_state *)),
  80.   gs_state *));
  81. private int expand_dashes(P3(subpath *, gx_path *, gs_state *));
  82. private void compute_caps(P1(pl_ptr));
  83. private int add_capped(P4(gx_path *, gs_line_cap,
  84.   int (*)(P3(gx_path *, fixed, fixed)),
  85.   ep_ptr));
  86.  
  87. /* Stroke a path for drawing or saving */
  88. int
  89. gx_stroke_fill(gx_path *ppath, gs_state *pgs)
  90. {    int code;
  91.     stroke_path = 0;
  92.     code = stroke(ppath, stroke_fill, pgs);
  93.     if ( stroke_path )        /* set if filling needed */
  94.       { if ( code >= 0 )
  95.           code = gx_fill_path(stroke_path, pgs->dev_color, pgs,
  96.                   gx_rule_winding_number, (fixed)0);
  97.         gx_path_release(stroke_path);
  98.       }
  99.     return code;
  100. }
  101. int
  102. gx_stroke_add(gx_path *ppath, gx_path *topath, gs_state *pgs)
  103. {    stroke_path = topath;
  104.     return stroke(ppath, stroke_add, pgs);
  105. }
  106.  
  107. /* Stroke a path.  Call line_proc (stroke_add or stroke_fill) */
  108. /* for each line segment. */
  109. private int
  110. stroke(gx_path *ppath,
  111.   int (*line_proc)(P4(int, pl_ptr, pl_ptr, gs_state *)),
  112.   gs_state *pgs)
  113. {    subpath *psub;
  114.     subpath *save_psub = 0;
  115.     int code = 0;
  116.     line_params *lp = pgs->line_params;
  117.     int dash_count = lp->dash.pattern_size;
  118.     gx_path fpath, dpath;
  119.     float xx = pgs->ctm.xx, xy = pgs->ctm.xy;
  120.     float yx = pgs->ctm.yx, yy = pgs->ctm.yy;
  121.     int skewed = !is_fzero2(xy, yx);
  122.     int uniform = (skewed ? 0 : xx == yy ? 1 : xx == -yy ? -1 : 0);
  123.     /*
  124.      * We are dealing with a reflected coordinate system
  125.      * if (1,0) is counter-clockwise from (0,1).
  126.      * See the note in stroke_add for the algorithm.
  127.      */    
  128.     int reflected =
  129.       (uniform ? uniform < 0 :
  130.        skewed ? xy * yx < xx * yy :
  131.        (xx < 0) == (yy < 0));
  132.     float line_width = lp->width;
  133.     int always_thin;
  134.     float line_width_and_scale;
  135. #ifdef DEBUG
  136. if ( gs_debug['o'] )
  137.    {    int count = lp->dash.pattern_size;
  138.     int i;
  139.     dprintf3("[o]half_width=%f, cap=%d, join=%d,\n",
  140.          lp->width, (int)lp->cap, (int)lp->join);
  141.     dprintf2("   miter_limit=%f, miter_check=%f,\n",
  142.          lp->miter_limit, lp->miter_check);
  143.     dprintf1("   dash pattern=%d", count);
  144.     for ( i = 0; i < count; i++ )
  145.         dprintf1(",%f", lp->dash.pattern[i]);
  146.     dprintf4(",\n   offset=%f, init(ink_on=%d, index=%d, dist_left=%f)\n",
  147.          lp->dash.offset, lp->dash.init_ink_on, lp->dash.init_index,
  148.          lp->dash.init_dist_left);
  149.    }
  150. #endif
  151.     if ( line_width < 0 ) line_width = -line_width;
  152.     if ( is_fzero(line_width) )
  153.         always_thin = 1;
  154.     else if ( !skewed )
  155.        {    float xxa = xx, yya = yy;
  156.         if ( xxa < 0 ) xxa = -xxa;
  157.         if ( yya < 0 ) yya = -yya;
  158.         always_thin = (max(xxa, yya) * line_width < 1.0);
  159.        }
  160.     else
  161.        {    /* The check is more complicated, but it's worth it. */
  162.         float xsq = xx * xx + xy * xy;
  163.         float ysq = yx * yx + yy * yy;
  164.         float cross = xx * yx + xy * yy;
  165.         if ( cross < 0 ) cross = 0;
  166.         always_thin =
  167.           ((max(xsq, ysq) + cross) * line_width * line_width < 1.0);
  168.        }
  169.     line_width_and_scale = line_width * (float)int2fixed(1);
  170. #ifdef DEBUG
  171. if ( gs_debug['o'] )
  172.     dprintf5("[o]ctm=(%g,%g,%g,%g) thin=%d\n",
  173.          xx, xy, yx, yy, always_thin);
  174. #endif
  175.     /* Start by flattening the path.  We should do this on-the-fly.... */
  176.     if ( !ppath->curve_count )    /* don't need to flatten */
  177.        {    psub = ppath->first_subpath;
  178.         if ( !psub ) return 0;
  179.        }
  180.     else
  181.        {    if ( (code = gx_path_flatten(ppath, &fpath, pgs->flatness)) < 0 ) return code;
  182.         psub = fpath.first_subpath;
  183.        }
  184.     if ( dash_count )
  185.         gx_path_init(&dpath, &ppath->memory_procs);
  186.     for ( ; ; )
  187.      { line_segment *pline;
  188.        fixed x, y;
  189.        partial_line pl, pl_prev, pl_first;
  190.        int first = 0;
  191.        int index = 0;
  192.        if ( !psub )
  193.         {    /* Might just be the end of a dash expansion. */
  194.         if ( save_psub )
  195.            {    gx_path_release(&dpath);
  196.             psub = (subpath *)save_psub->last->next;
  197.             if ( !psub ) break;
  198.             gx_path_init(&dpath, &ppath->memory_procs);
  199.             save_psub = 0;
  200.            }
  201.         else        /* all done */
  202.             break;
  203.         }
  204.        if ( dash_count && !save_psub )
  205.         {    code = expand_dashes(psub, &dpath, pgs);
  206.         if ( code < 0 ) goto exit;
  207.         save_psub = (subpath *)psub;
  208.         psub = dpath.first_subpath;
  209.         continue;        /* psub might be null */
  210.         }
  211.        pline = (line_segment *)(psub->next);
  212.        x = psub->pt.x;
  213.        y = psub->pt.y;
  214.        while ( pline != 0 && pline->type != s_start )
  215.         {    fixed sx = pline->pt.x;
  216.         fixed sy = pline->pt.y;
  217.         /* Compute the width parameters in device space. */
  218.         /* We work with unscaled values, for speed. */
  219.         pl.o.p.x = x, pl.o.p.y = y;
  220.         pl.e.p.x = sx, pl.e.p.y = sy;
  221.         if ( !always_thin )
  222.            {    fixed udx = sx - x, udy = sy - y;
  223.             if ( !(udx | udy) )    /* degenerate */
  224.              { /* Only consider a degenerate segment */
  225.                /* if the entire subpath is degenerate and */
  226.                /* we are using round caps or joins. */
  227.                if ( index != 0 || (pline->next != 0 &&
  228.                  pline->next->type != s_start) ||
  229.                 (lp->cap != gs_cap_round &&
  230.                  lp->join != gs_join_round)
  231.                   )
  232.                  goto nd;
  233.                /* Pick an arbitrary orientation. */
  234.                udx = int2fixed(1);
  235.              }
  236.             if ( uniform != 0 )
  237.                {    /* We can save a lot of work in this case. */
  238.                 float dpx = udx, dpy = udy;
  239.                 float wl = line_width_and_scale * xx /
  240.                     hypot(dpx, dpy);
  241.                 pl.e.cdelta.x = (fixed)(dpx * wl);
  242.                 pl.e.cdelta.y = (fixed)(dpy * wl);
  243.                 if ( uniform > 0 )    /* xx == yy */
  244.                    {    pl.width.x = pl.e.cdelta.y;
  245.                     pl.width.y = -pl.e.cdelta.x;
  246.                    }
  247.                 else            /* xx == -yy */
  248.                    {    pl.width.x = -pl.e.cdelta.y;
  249.                     pl.width.y = pl.e.cdelta.x;
  250.                    }
  251.                }
  252.             else
  253.                {    gs_point dpt;    /* unscaled */
  254.                 float wl;
  255.                 if ( skewed )
  256.                     gs_idtransform(pgs,
  257.                           (float)udx, (float)udy, &dpt);
  258.                 else    /* shortcut */
  259.                     dpt.x = udx / xx,
  260.                     dpt.y = udy / yy;
  261.                 wl = line_width_and_scale /
  262.                     hypot(dpt.x, dpt.y);
  263.                 /* Construct the width vector in */
  264.                 /* user space, still unscaled. */
  265.                 dpt.x *= wl;
  266.                 dpt.y *= wl;
  267.                 /*
  268.                  * We now compute both perpendicular
  269.                  * and (optionally) parallel half-widths,
  270.                  * as deltas in device space.  We use
  271.                  * a fixed-point, unscaled version of
  272.                  * gs_dtransform.  The second computation
  273.                  * folds in a 90-degree rotation (in user
  274.                  * space, before transforming) in the
  275.                  * direction that corresponds to clockwise
  276.                  * in device space.
  277.                  */
  278.                 pl.e.cdelta.x = (fixed)(dpt.x * xx);
  279.                 pl.e.cdelta.y = (fixed)(dpt.y * yy);
  280.                 if ( skewed )
  281.                   pl.e.cdelta.x += (fixed)(dpt.y * yx),
  282.                   pl.e.cdelta.y += (fixed)(dpt.x * xy);
  283.                 if ( reflected )
  284.                   dpt.x = -dpt.x, dpt.y = -dpt.y;
  285.                 pl.width.x = (fixed)(dpt.y * xx),
  286.                 pl.width.y = -(fixed)(dpt.x * yy);
  287.                 if ( skewed )
  288.                   pl.width.x -= (fixed)(dpt.x * yx),
  289.                   pl.width.y += (fixed)(dpt.y * xy);
  290.                }
  291.             pl.thin =
  292.               any_abs(pl.width.x) + any_abs(pl.width.y) <
  293.                 float2fixed(0.75);
  294.             if ( !pl.thin ) compute_caps(&pl);
  295.            }
  296.         else
  297.             pl.e.cdelta.x = pl.e.cdelta.y = 0,
  298.             pl.width.x = pl.width.y = 0,
  299.             pl.thin = 1;
  300.         if ( first++ == 0 ) pl_first = pl;
  301.         if ( index++ ) (*line_proc)(index - 2, &pl_prev, &pl, pgs);
  302.         pl_prev = pl;
  303.         x = sx, y = sy;
  304. nd:        pline = (line_segment *)(pline->next);
  305.         }
  306.        if ( index )
  307.         {    /* If closed, join back to start, else cap */
  308.         (*line_proc)(index - 1, &pl_prev,
  309.                  (psub->closed ? &pl_first : (pl_ptr)0), pgs);
  310.         }
  311.        psub = (subpath *)pline;
  312.        if ( stroke_path == &stroke_path_body )
  313.         {    /* Fill and release the accumulated path */
  314.         gx_fill_path(stroke_path, pgs->dev_color, pgs,
  315.                  gx_rule_winding_number, (fixed)0);
  316.         gx_path_release(stroke_path);
  317.         stroke_path = 0;
  318.         }
  319.      }
  320. exit:    if ( dash_count ) gx_path_release(&dpath);
  321.     if ( ppath->curve_count ) gx_path_release(&fpath);
  322.     return code;
  323. }
  324.  
  325. /* ------ Internal routines ------ */
  326.  
  327. /* Expand a dashed subpath into explicit segments. */
  328. /* The subpath contains no curves. */
  329. private int
  330. expand_dashes(subpath *psub, gx_path *ppath, gs_state *pgs)
  331. {    int skewed = is_skewed(&pgs->ctm);
  332.     dash_params *dash = &pgs->line_params->dash;
  333.     float *pattern = dash->pattern;
  334.     int count, ink_on, index;
  335.     float dist_left;
  336.     fixed x0 = psub->pt.x, y0 = psub->pt.y;
  337.     fixed x, y;
  338.     segment *pseg;
  339.     int wrap = (dash->init_ink_on && psub->closed ? -1 : 0);
  340.     int drawing = wrap;
  341.     int code;
  342.     if ( (code = gx_path_add_point(ppath, x0, y0)) < 0 )
  343.         return code;
  344.     /* To do the right thing at the beginning of a closed path, */
  345.     /* we have to skip any initial line, and then redo it at */
  346.     /* the end of the path.  Drawing = -1 while skipping, */
  347.     /* 0 while drawing normally, and 1 on the second round. */
  348. top:    count = dash->pattern_size;
  349.     ink_on = dash->init_ink_on;
  350.     index = dash->init_index;
  351.     dist_left = dash->init_dist_left;
  352.     x = x0, y = y0;
  353.     pseg = (segment *)psub;
  354.     while ( (pseg = pseg->next) != 0 && pseg->type != s_start )
  355.        {    fixed sx = pseg->pt.x, sy = pseg->pt.y;
  356.         fixed udx = sx - x, udy = sy - y;
  357.         float length, dx, dy;
  358.         float dist;
  359.         if ( !(udx | udy) )    /* degenerate */
  360.             dx = 0, dy = 0, length = 0;
  361.         else
  362.            {    gs_point d;
  363.             dx = udx, dy = udy;    /* scaled as fixed */
  364.             if ( skewed )
  365.                 gs_idtransform(pgs, dx, dy, &d);
  366.             else    /* shortcut */
  367.                 d.x = dx / pgs->ctm.xx,
  368.                 d.y = dy / pgs->ctm.yy;
  369.             length = sqrt(d.x * d.x + d.y * d.y) *
  370.                    (1 / (float)int2fixed(1));
  371.            }
  372.         dist = length;
  373.         while ( dist > dist_left )
  374.            {    /* We are using up the dash element */
  375.             float fraction = dist_left / length;
  376.             fixed nx = x + (fixed)(dx * fraction);
  377.             fixed ny = y + (fixed)(dy * fraction);
  378.             if ( ink_on )
  379.                {    if ( drawing >= 0 )
  380.                   code = gx_path_add_line(ppath, nx, ny);
  381.                }
  382.             else
  383.                {    if ( drawing > 0 ) return 0;    /* done */
  384.                 code = gx_path_add_point(ppath, nx, ny);
  385.                 drawing = 0;
  386.                }
  387.             if ( code < 0 ) return code;
  388.             dist -= dist_left;
  389.             ink_on = !ink_on;
  390.             if ( ++index == count ) index = 0;
  391.             dist_left = pattern[index];
  392.             x = nx, y = ny;
  393.            }
  394.         dist_left -= dist;
  395.         /* Handle the last dash of a segment. */
  396.         if ( ink_on )
  397.            {    if ( drawing >= 0 )
  398.               code =
  399.                 (pseg->type == s_line_close && drawing > 0 ?
  400.                  gx_path_close_subpath(ppath) :
  401.                  gx_path_add_line(ppath, sx, sy));
  402.            }
  403.         else
  404.            {    if ( drawing > 0 ) return 0;    /* done */
  405.             code = gx_path_add_point(ppath, sx, sy);
  406.             drawing = 0;
  407.            }
  408.         if ( code < 0 ) return code;
  409.         x = sx, y = sy;
  410.        }
  411.     /* Check for wraparound. */
  412.     if ( wrap && drawing <= 0 )
  413.        {    /* We skipped some initial lines. */
  414.         /* Go back and do them now. */
  415.         drawing = 1;
  416.         goto top;
  417.        }
  418.     return 0;
  419. }
  420.  
  421. /* Compute the intersection of two lines.  This is a messy algorithm */
  422. /* that somehow ought to be useful in more places than just here.... */
  423. private void
  424. line_intersect(
  425.     p_ptr pp1,                /* point on 1st line */
  426.     p_ptr pd1,                /* slope of 1st line (dx,dy) */
  427.     p_ptr pp2,                /* point on 2nd line */
  428.     p_ptr pd2,                /* slope of 2nd line */
  429.     p_ptr pi)                /* return intersection here */
  430. {    /* We don't have to do any scaling, the factors all work out right. */
  431.     float u1 = pd1->x, v1 = pd1->y;
  432.     float u2 = pd2->x, v2 = pd2->y;
  433.     double denom = u1 * v2 - u2 * v1;
  434.     double num1 = v1 * pp1->x - u1 * pp1->y;
  435.     double num2 = v2 * pp2->x - u2 * pp2->y;
  436.     double xnum = u1 * num2 - u2 * num1;
  437.     double ynum = v1 * num2 - v2 * num1;
  438.     double max_result = any_abs(denom) * (double)max_fixed;
  439. #ifdef DEBUG
  440. if ( gs_debug['o'] )
  441.    {    dprintf4("[o]Intersect %f,%f(%f/%f)",
  442.          fixed2float(pp1->x), fixed2float(pp1->y),
  443.          fixed2float(pd1->x), fixed2float(pd1->y));
  444.     dprintf4(" & %f,%f(%f/%f),\n",
  445.          fixed2float(pp2->x), fixed2float(pp2->y),
  446.          fixed2float(pd2->x), fixed2float(pd2->y));
  447.     dprintf4("\txnum=%f ynum=%f denom=%f max_result=%f ->\n",
  448.          xnum, ynum, denom, max_result);
  449.    }
  450. #endif
  451.     /* Check for degenerate result. */
  452.     if ( denom == 0 || any_abs(xnum) > max_result || any_abs(ynum) > max_result )
  453.        {    /* The lines are nearly parallel, */
  454.         /* or one of them has zero length.  Punt. */
  455.         *pi = *pp1;
  456. #ifdef DEBUG
  457. if ( gs_debug['o'] )
  458.         dprintf("\tdegenerate!\n");
  459. #endif
  460.        }
  461.     else
  462.        {    pi->x = (fixed)(xnum / denom);
  463.         pi->y = (fixed)(ynum / denom);
  464. #ifdef DEBUG
  465. if ( gs_debug['o'] )
  466.     dprintf2("\t%f,%f\n", fixed2float(pi->x), fixed2float(pi->y));
  467. #endif
  468.        }
  469. }
  470.  
  471. #define lix plp->o.p.x
  472. #define liy plp->o.p.y
  473. #define litox plp->e.p.x
  474. #define litoy plp->e.p.y
  475.  
  476. /* Draw a line on the device. */
  477. private int
  478. stroke_fill(int first, register pl_ptr plp, pl_ptr nplp, gs_state *pgs)
  479. {    if ( plp->thin )
  480.        {    /* Minimum-width line, don't have to be careful. */
  481.         /* We do have to check for the entire line being */
  482.         /* within the clipping rectangle, allowing for some */
  483.         /* slop at the ends. */
  484.         fixed dx = litox - lix, dy = litoy - liy;
  485. #define trsign(v, c) (v >= 0 ? c : -c)
  486. #define slop int2fixed(2)
  487.         fixed xslop = trsign(dx, slop);
  488.         fixed yslop = trsign(dy, slop);
  489.         if ( gx_cpath_includes_rectangle(pgs->clip_path,
  490.                 lix - xslop, liy - yslop,
  491.                 litox + xslop, litoy + yslop) )
  492.             return gz_draw_line_fixed(lix, liy, litox, litoy,
  493.                 pgs->dev_color, pgs);
  494. #undef slop
  495.         /* We didn't set up the endpoint parameters before, */
  496.         /* because the line was thin.  Do it now. */
  497.         /* We only approximate the width and height. */
  498.         if ( any_abs(dx) > any_abs(dy) )
  499.            {    plp->width.x = plp->e.cdelta.y = 0;
  500.             plp->width.y = -(plp->e.cdelta.x =
  501.                 trsign(dx, float2fixed(-0.5)));
  502.            }
  503.         else
  504.            {    plp->width.y = plp->e.cdelta.x = 0;
  505.             plp->width.x = -(plp->e.cdelta.y =
  506.                 trsign(dy, float2fixed(-0.5)));
  507.            }
  508. #undef trsign
  509.         compute_caps(plp);
  510.        }
  511.        {    /* General case. */
  512.         /* Construct a path and hand it to the fill algorithm. */
  513.         if ( stroke_path == 0 )
  514.            {    /* We are rendering, and haven't run into the */
  515.             /* general case yet.  Initialize the path. */
  516.             stroke_path = &stroke_path_body;    /* set global for stroke_add */
  517.             gx_path_init(stroke_path, &pgs->memory_procs);
  518.            }
  519.         stroke_add(first, plp, nplp, pgs);
  520.            {    /****** PATCH ******/
  521.             if ( stroke_path == &stroke_path_body )
  522.                {    gx_fill_path(stroke_path, pgs->dev_color, pgs,
  523.                          gx_rule_winding_number, (fixed)0);
  524.                 gx_path_release(stroke_path);
  525.                 stroke_path = 0;
  526.                }
  527.            }
  528.        }
  529.     return 0;
  530. }
  531.  
  532. #undef lix
  533. #undef liy
  534. #undef litox
  535. #undef litoy
  536.  
  537. /* Add a segment to the path.  This handles all the complex cases. */
  538. private int add_capped(P4(gx_path *, gs_line_cap, int (*)(P3(gx_path *, fixed, fixed)), ep_ptr));
  539. private int
  540. stroke_add(int first, register pl_ptr plp, pl_ptr nplp, gs_state *pgs)
  541. {    gx_path *ppath = stroke_path;
  542.     int code;
  543.     if ( ppath == 0 ) return 0;    /****** strokepath is NYI ******/
  544.     if ( plp->thin )
  545.        {    /* We didn't set up the endpoint parameters before, */
  546.         /* because the line was thin.  Do it now. */
  547.         compute_caps(plp);
  548.        }
  549.     if ( (code = add_capped(ppath, (first == 0 ? pgs->line_params->cap : gs_cap_butt), gx_path_add_point, &plp->o)) < 0 )
  550.         return code;
  551.     if ( nplp == 0 )
  552.        {    code = add_capped(ppath, pgs->line_params->cap, gx_path_add_line, &plp->e);
  553.        }
  554.     else if ( pgs->line_params->join == gs_join_round )
  555.        {    code = add_capped(ppath, gs_cap_round, gx_path_add_line, &plp->e);
  556.        }
  557.     else if ( nplp->thin )        /* no join */
  558.       {    code = add_capped(ppath, gs_cap_butt, gx_path_add_line, &plp->e);
  559.       }
  560.     else                /* join_bevel or join_miter */
  561.        {    gs_fixed_point jp1, jp2;
  562.         /*
  563.          * Set np to whichever of nplp->o.co or .ce
  564.          * is outside the current line.
  565.          * We use the interesting observation that
  566.          * point (x2,y2) is counter-clockwise from (x1,y1)
  567.          * relative to the origin iff x1*y2 < x2*y1.
  568.          * In this case x1,y1 are plp->width,
  569.          * x2,y2 are nplp->width, and the origin is
  570.          * their common point (plp->e.p, nplp->o.p).
  571.          */
  572.         float wx1 = plp->width.x, wy1 = plp->width.y;
  573.         float wx2 = nplp->width.x, wy2 = nplp->width.y;
  574.         int ccw = wx1 * wy2 < wx2 * wy1;
  575.         p_ptr outp, np, np1, np2;
  576.         /* Initialize for a bevel join. */
  577.         jp1.x = plp->e.co.x, jp1.y = plp->e.co.y;
  578.         jp2.x = plp->e.ce.x, jp2.y = plp->e.ce.y;
  579.         if ( ccw )
  580.             outp = &jp2, np2 = np = &nplp->o.co, np1 = &plp->e.p;
  581.         else
  582.             outp = &jp1, np1 = np = &nplp->o.ce, np2 = &plp->e.p;
  583. #ifdef DEBUG
  584. if ( gs_debug['o'] )
  585.         dprintf1("[o]use %s\n", (ccw ? "co (ccw)" : "ce (cw)"));
  586. #endif
  587.         /* Don't bother with the miter check if the two */
  588.         /* points to be joined are very close together, */
  589.         /* namely, in the same square half-pixel. */
  590.         if ( pgs->line_params->join == gs_join_miter &&
  591.              !(fixed2long(outp->x << 1) == fixed2long(np->x << 1) &&
  592.                fixed2long(outp->y << 1) == fixed2long(np->y << 1))
  593.            )
  594.           { /*
  595.              * Check whether a miter join is appropriate.
  596.              * Let a, b be the angles of the two lines.
  597.              * We check tan(a-b) against the miter_check
  598.              * by using the following formula:
  599.              * If tan(a)=u1/v1 and tan(b)=u2/v2, then
  600.              * tan(a-b) = (u1*v2 - u2*v1) / (u1*u2 + v1*v2).
  601.              * We can do all the computations unscaled,
  602.              * because we're only concerned with ratios.
  603.              */
  604.             float u1 = plp->e.cdelta.x, v1 = plp->e.cdelta.y;
  605.             float u2 = nplp->o.cdelta.x, v2 = nplp->o.cdelta.y;
  606.             float num = u1 * v2 - u2 * v1;
  607.             float denom = u1 * u2 + v1 * v2;
  608.             float check = pgs->line_params->miter_check;
  609.             /*
  610.              * We will want either tan(a-b) or tan(b-a)
  611.              * depending on the orientations of the lines.
  612.              * Fortunately we know the relative orientations already.
  613.              */
  614.             if ( !ccw )        /* have plp - nplp, want vice versa */
  615.             num = -num;
  616. #ifdef DEBUG
  617. if ( gs_debug['o'] )
  618.                    {    dprintf4("[o]Miter check: u1/v1=%f/%f, u2/v2=%f/%f,\n",
  619.                  u1, v1, u2, v2);
  620.                         dprintf3("        num=%f, denom=%f, check=%f\n",
  621.                  num, denom, check);
  622.                    }
  623. #endif
  624.             /* Use a miter if num / denom >= check. */
  625.             /* If check > 0, num < 0 always passes; */
  626.             /* if check < 0, num >= 0 always fails. */
  627.             if ( denom < 0 ) num = -num, denom = -denom;
  628.             if ( check > 0 ?
  629.             (num < 0 || num >= denom * check) :
  630.             (num < 0 && num >= denom * check)
  631.                )
  632.                {    /* OK to use a miter join. */
  633. #ifdef DEBUG
  634. if ( gs_debug['o'] )
  635.                 dputs("        ... passes.\n");
  636. #endif
  637.                 /* Compute the intersection of */
  638.                 /* the extended edge lines. */
  639.                 line_intersect(outp, &plp->e.cdelta, np,
  640.                            &nplp->o.cdelta, outp);
  641.                }
  642.            }
  643.         if ( (code = gx_path_add_line(ppath, jp1.x, jp1.y)) < 0 ||
  644.              (code = gx_path_add_line(ppath, np1->x, np1->y)) < 0 ||
  645.              (code = gx_path_add_line(ppath, np2->x, np2->y)) < 0 ||
  646.              (code = gx_path_add_line(ppath, jp2.x, jp2.y)) < 0
  647.            )
  648.             return code;
  649.        }
  650.     if ( code < 0 || (code = gx_path_close_subpath(ppath)) < 0 )
  651.         return code;
  652.     return 0;
  653. }
  654.  
  655. /* Routines for cap computations */
  656.  
  657. /* Compute the endpoints of the two caps of a segment. */
  658. private void
  659. compute_caps(register pl_ptr plp)
  660. {    fixed wx2 = plp->width.x;
  661.     fixed wy2 = plp->width.y;
  662.     plp->o.co.x = plp->o.p.x + wx2, plp->o.co.y = plp->o.p.y + wy2;
  663.     plp->o.cdelta.x = -plp->e.cdelta.x,
  664.       plp->o.cdelta.y = -plp->e.cdelta.y;
  665.     plp->o.ce.x = plp->o.p.x - wx2, plp->o.ce.y = plp->o.p.y - wy2;
  666.     plp->e.co.x = plp->e.p.x - wx2, plp->e.co.y = plp->e.p.y - wy2;
  667.     plp->e.ce.x = plp->e.p.x + wx2, plp->e.ce.y = plp->e.p.y + wy2;
  668. #ifdef DEBUG
  669. if ( gs_debug['o'] )
  670.     dprintf4("[o]Stroke o=(%f,%f) e=(%f,%f)\n",
  671.          fixed2float(plp->o.p.x), fixed2float(plp->o.p.y),
  672.          fixed2float(plp->e.p.x), fixed2float(plp->e.p.y)),
  673.     dprintf4("\twxy=(%f,%f) lxy=(%f,%f)\n",
  674.          fixed2float(wx2), fixed2float(wy2),
  675.          fixed2float(plp->e.cdelta.x), fixed2float(plp->e.cdelta.y));
  676. #endif
  677. }
  678.  
  679. /* Add a properly capped line endpoint to the path. */
  680. /* The first point may require either moveto or lineto. */
  681. private int
  682. add_capped(gx_path *ppath, gs_line_cap type,
  683.   int (*add_proc)(P3(gx_path *, fixed, fixed)), /* gx_path_add_point/line */
  684.   register ep_ptr endp)
  685. {    int code;
  686. #define px endp->p.x
  687. #define py endp->p.y
  688. #define xo endp->co.x
  689. #define yo endp->co.y
  690. #define xe endp->ce.x
  691. #define ye endp->ce.y
  692. #define cdx endp->cdelta.x
  693. #define cdy endp->cdelta.y
  694. #ifdef DEBUG
  695. if ( gs_debug['o'] )
  696.     dprintf4("[o]cap: p=(%g,%g), co=(%g,%g),\n",
  697.          fixed2float(px), fixed2float(py),
  698.          fixed2float(xo), fixed2float(yo)),
  699.     dprintf4("[o]\tce=(%g,%g), cd=(%g,%g)\n",
  700.          fixed2float(xe), fixed2float(ye),
  701.          fixed2float(cdx), fixed2float(cdy));
  702. #endif
  703.     switch ( type )
  704.        {
  705.     case gs_cap_round:
  706.        {    fixed xm = px + cdx;
  707.         fixed ym = py + cdy;
  708.         if (    (code = (*add_proc)(ppath, xo, yo)) < 0 ||
  709.             (code = gx_path_add_arc(ppath, xo, yo, xm, ym,
  710.                 xo + cdx, yo + cdy, quarter_arc_fraction)) < 0 ||
  711.             (code = gx_path_add_arc(ppath, xm, ym, xe, ye,
  712.                 xe + cdx, ye + cdy, quarter_arc_fraction)) < 0
  713.            ) return code;
  714.        }
  715.         break;
  716.     case gs_cap_square:
  717.         if (    (code = (*add_proc)(ppath, xo + cdx, yo + cdy)) < 0 ||
  718.             (code = gx_path_add_line(ppath, xe + cdx, ye + cdy)) < 0
  719.            ) return code;
  720.         break;
  721.     case gs_cap_butt:
  722.         if (    (code = (*add_proc)(ppath, xo, yo)) < 0 ||
  723.             (code = gx_path_add_line(ppath, xe, ye)) < 0
  724.            ) return code;
  725.        }
  726.     return code;
  727. }
  728.